package com.ctrip.cap.domain.benchmark;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;



public class ActivityStartInfo   {
	public double getAverage() {
		average=0;
		int totaltimes=0;
		Iterator iter = runningStatus.entrySet().iterator();
		while (iter.hasNext()) {
			Map.Entry entry = (Map.Entry) iter.next();
			ActivityStartTimeInfo val = (ActivityStartTimeInfo) entry.getValue();
			average+=val.getNum()*val.getTime();
			totaltimes+=val.getNum();
		}
		if(totaltimes==0)
		{
			return -1;
		}
		return average/totaltimes;
	}
	public String getActivityName() {
		return activityName;
	}
	public void setActivityName(String activityName) {
		this.activityName = activityName;
	}
	public int getStartTimes() {
		return startTimes;
	}
	public void setStartTimes(int startTimes) {
		this.startTimes = startTimes;
	}
	public HashMap<String, ActivityStartTimeInfo> getRunningStatus() {
		if(runningStatus==null)
		{
			runningStatus=new HashMap<String, ActivityStartTimeInfo>();
		}
		return runningStatus;
	}
	public void setRunningStatus(HashMap<String, ActivityStartTimeInfo> runningStatus) {
		this.runningStatus = runningStatus;
	}
	private double average;
	private String activityName;
	private int startTimes;
	private HashMap<String,ActivityStartTimeInfo> runningStatus;
	public ActivityStartInfo diff(ActivityStartInfo activityInfo)
	{
		if(!this.activityName.equals(activityInfo.getActivityName()))
		{
			return null;
		}
		ActivityStartInfo info=new ActivityStartInfo();
		info.setActivityName(activityName);
		info.setStartTimes(startTimes-activityInfo.getStartTimes());
		HashMap<String,ActivityStartTimeInfo> newStatus=new HashMap<String,ActivityStartTimeInfo>();
		HashMap<String,ActivityStartTimeInfo> oldStatus=activityInfo.getRunningStatus();
		Iterator iter = runningStatus.entrySet().iterator();
		while (iter.hasNext()) {
			Map.Entry entry = (Map.Entry) iter.next();
			String key = (String) entry.getKey();
			ActivityStartTimeInfo val = (ActivityStartTimeInfo) entry.getValue();
			if(oldStatus.containsKey(key))
			{
				int n=val.getNum()-oldStatus.get(key).getNum();
				val.setNum(n);
			}
			if(val.getNum()!=0)
			{
				newStatus.put(key, val);
			}
		}
		info.setRunningStatus(newStatus);
		return info;
	}
	
}
